package com.hhf.rpc.core.server.impl;

import com.hhf.rpc.core.handler.TcpServerHandler;
import com.hhf.rpc.core.server.HttpServer;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author hhf
 * @Date 2024/10/19
 * @Description
 */
@Slf4j
public class VertxTcpServer implements HttpServer {



    @Override
    public void doStart(int port) {
        //1.创建实例
        Vertx vertx = Vertx.vertx();
        NetServer server = vertx.createNetServer();
        //2.添加请求处理
        server.connectHandler(new TcpServerHandler());
        //3.监听端口
        server.listen(port,result -> {
            if (result.succeeded()){
                log.info("vertx服务器启动成功,正在监听端口:{}",port);
            }else {
                log.error("vertx服务器启动失败,报错信息:{}",result.cause());
            }
        });
    }

    private byte[] handleRequest(byte[] requestData){
        String requestStr = requestData.toString();
        String result = requestStr + "请求处理完成";
        return result.getBytes();
    }


    public static void main(String[] args) {
        new VertxTcpServer().doStart(8888);
    }
}
